package com.dyb.nio.reactor;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 * 客户端
 * @author dyb
 * @date 2020/6/19
 */
public class Client {

    public static void main(String[] args) throws IOException {
        // 启动
        new Client().start();
    }

    private void start() throws IOException {
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8888);
        SocketChannel socketChannel = SocketChannel.open(inetSocketAddress);
        socketChannel.configureBlocking(false);
        // 连接
        while (!socketChannel.finishConnect()) {}
        System.out.println("客户端启动完成");
        // 启动
        new Thread(new Processer(socketChannel)).start();
    }

    class Processer implements Runnable {
        SocketChannel socketChannel;
        Selector selector;
        ByteBuffer byteBuffer;

        Processer (SocketChannel socketChannel) throws IOException {
            this.socketChannel = socketChannel;
            selector = Selector.open();
            socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
            byteBuffer = ByteBuffer.allocate(1024);
        }

        @Override
        public void run() {
            try {
                while (!Thread.interrupted()) {
                    selector.select();
                    Set<SelectionKey> selectionKeys = selector.selectedKeys();
                    Iterator<SelectionKey> iterator = selectionKeys.iterator();
                    while (iterator.hasNext()) {
                        SelectionKey selectionKey = iterator.next();
                        if (selectionKey.isReadable()) {
                            int length = 0;
                            while ((length = socketChannel.read(byteBuffer)) > 0) {
                                // 切换到可读模式
                                System.out.println("server echo："+ new String(byteBuffer.array(), 0, length));
                            }
                            byteBuffer.clear();
                        } else if (selectionKey.isWritable()) {
                            System.out.println("请输入发送内容：");
                            Scanner scanner = new Scanner(System.in);
                            if (scanner.hasNext()) {
                                String content = scanner.nextLine();
                                byteBuffer.put(content.getBytes());
                                // 切换为可读
                                byteBuffer.flip();
                                // 发送数据
                                socketChannel.write(byteBuffer);
                                // 清空
                                byteBuffer.clear();
                            }
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

